/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.jx.web;

//import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
//import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.food.entity.FoodExpenditureRecords;
import com.thinkgem.jeesite.modules.food.entity.FoodInfo;
import com.thinkgem.jeesite.modules.food.entity.FoodSupplier;
import com.thinkgem.jeesite.modules.qc.entity.Financial;
import com.thinkgem.jeesite.modules.qc.entity.SeniorAssets;
import com.thinkgem.jeesite.modules.jx.entity.ProjectPerformanceReport;
import com.thinkgem.jeesite.modules.jx.entity.SecurityDriver;
import com.thinkgem.jeesite.modules.jx.service.SecurityDriverService;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
//import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.test.entity.ReturnData;

/**
 * 供应商信息Controller
 * @author  
 * @version  
 */
@Controller
@RequestMapping(value = "${adminPath}/jx/securityDriver")
public class SecurityDriverController extends BaseController {

	@Autowired
	private SecurityDriverService securityDriverService;
	private SecurityDriver securityDriver;
/*	private String entryTime;*/
	
	@ModelAttribute
	public SecurityDriver get(@RequestParam(required=false) String id) {
		SecurityDriver entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = securityDriverService.get(id);
		}
		if (entity == null){
			entity = new SecurityDriver();
		}
		return entity;
	}
	
	 @RequestMapping(value = {"list", ""})
	public String list(SecurityDriver securityDriver, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SecurityDriver> page = securityDriverService.findPage(new Page<SecurityDriver>(request, response), securityDriver); 
		model.addAttribute("page", page);
		return "modules/jx/securityDriverList";
	} 
	 

	@ResponseBody
	@RequestMapping(value = {"listByAjax"})
	public String listByAjax(HttpServletRequest request, HttpServletResponse response, Model model)throws ParseException  {
		String companyName = request.getParameter("companyName");
		String vehicleNo = request.getParameter("vehicleNo");
	 	String entryTime = request.getParameter("entryTime"); 
		
		SecurityDriver securityDriver = new SecurityDriver();
	/*	  String officeId = request.getParameter("officeId"); */
		securityDriver.setCompanyName(companyName);
	 	securityDriver.setVehicleNo(vehicleNo);
		
/*		seniorAssets.setPhone(phone);*/
	/*	Office office = new Office();
		office.setId(officeId);*/
		if(entryTime != null && !entryTime.equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			securityDriver.setEntryTime(sdf.parse(entryTime));
		/*	securityDriver.setVehicleNo(sdf.parse(vehicleNo));*/
		} 
		
		
		List<SecurityDriver> securityDriverList = securityDriverService.findList(securityDriver);
		
		ReturnData returnData = new ReturnData();
		returnData.setCode("0");
		returnData.setCount(String.valueOf(securityDriverList.size()));
		returnData.setData(securityDriverList);
		String jsonData = JSON.toJSONString(returnData);
		System.out.println(jsonData);
		return jsonData;
	}
	
	  
	@RequestMapping(value = "form")
	public String form(SecurityDriver securityDriver, Model model) {
		model.addAttribute("SecurityDriver", securityDriver);
		return "modules/jx/securityDriverForm";
	}
	
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(String dataString, Model model, RedirectAttributes redirectAttributes) {
			dataString = StringEscapeUtils.unescapeHtml4(dataString);
			/*ProjectPerformanceReport projectPerformanceReport = UserUtils.getUser().getOffice();*/
			SecurityDriver securityDriver = JSON.parseObject(dataString,SecurityDriver.class);
		/*	securityDriver.setOfficeId(UserUtils.getUser().getOffice());//office定义赋值。
*/			securityDriverService.save(securityDriver);
			String status =  JSON.toJSONString("保存数据成功");
			return status;
		}
	
	
	/**
	 * 导出用户数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "btnexport", method=RequestMethod.POST)
    public String export(SecurityDriver securityDriver, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String export = "用户财产数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
       	 	List<SecurityDriver> list = securityDriverService.findList(securityDriver);
//            List<Financial> list = financialService.findList(financial);
     		new ExportExcel("用户财产数据", Financial.class).setDataList(list).write(response, export).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出用户失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/jx/securityDriver/list?repage";
    }

	@ResponseBody
	@RequestMapping(value = "deleteById")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		SecurityDriver securityDriver = new SecurityDriver();
		securityDriver.setId(id);
		securityDriverService.delete(securityDriver);
		String status =  JSON.toJSONString("删除伙食支出成功");
		return status;
	}

}